import java.util.Random;

/**
 * A subclass of java.util.Random to allow for unit testing of code that uses
 * random numbers.
 * 
 * This class has a sequence of true-false values that are provided in place of
 * random values by nextBoolean(). When the last of the values is reached, the
 * sequence is repeated.
 */
@SuppressWarnings("serial")
public class TestRandom extends Random {
	private boolean[] values = { false, true, true, false, false, true, false };
	private int index;

	/**
	 * Default constructor to use the default sequence of values: false, true,
	 * true, false, false, true, false.
	 */
	public TestRandom() {

	}

	/**
	 * Constructor to use the given sequence of values.
	 * 
	 * @param values
	 *            The sequence of values to provide
	 */
	public TestRandom(boolean... values) {
		this.values = values;
	}

	/**
	 * Returns the next value in the sequence.
	 */
	@Override
	public boolean nextBoolean() {
		if (index >= values.length) {
			index = 0;
		}

		return values[index++];
	}

	/**
	 * Sets the sequence of values to use.
	 * 
	 * @param values
	 *            The new sequence of values
	 */
	public void setValues(boolean[] values) {
		this.values = values;
	}
}
